local zhantu = fk.CreateSkill {
  name = "inker_zhantu",
  tags = { Skill.Hidden }
}

local U = require "packages.utility.utility"

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: TriggerEvent, target: ServerPlayer, player: ServerPlayer, data: TriggerData):any>
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhantu.name) then
      return not player:isNude()
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getCardIds("he")
    local craftys = table.filter(cards, function(cid)
      return Fk:getCardById(cid).trueName == "crafty_escape"
    end)
    if #craftys > 0 then
      room:throwCard(craftys, zhantu.name, player, player)
      cards = player:getCardIds("he")
    end
    if #cards == 0 then return end
    local choosed = room:askToDiscard(player, {
      min_num = 1,
      max_num = #cards,
      include_equip = true,
      skill_name = zhantu.name,
      prompt = "#inker_zhantu-invoke",
    })
    if #choosed == 0 then return end
    local banner = room:getBanner(zhantu.name)
    if banner == nil then
      banner = {}
      for _, card in ipairs(Fk.cards) do
        if card.trueName == "crafty_escape" then
          table.insert(banner, { card.suit, card.number })
        end
      end
      room:setBanner(zhantu.name, banner)
    end
    local pick = table.random(banner)
    local c = room:printCard("crafty_escape",pick[1], pick[2])
    room:setCardMark(c, MarkEnum.DestructIntoDiscard, 1)
    room:moveCardTo(c, Card.PlayerHand, player, fk.ReasonJustMove, zhantu.name, nil, true, player)
  end
}

zhantu:addEffect(U.GeneralAppeared, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhantu.name) and not player:isNude() then
      if data.reason ~= "HpChanged" then
        return true
      else
        local logic = player.room.logic
        local hpevent = logic:getCurrentEvent():findParent(GameEvent.ChangeHp, true)
        return not (hpevent and hpevent.data.damageEvent)
      end
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use
})
zhantu:addEffect(fk.Damaged, spec)

Fk:loadTranslationTable{
  ["inker_zhantu"] = "辗途",
  [":inker_zhantu"] = "隐匿。当你不因受伤登场或受到伤害后，你弃置所有的【金蝉脱壳】，然后可以弃置任意张牌并从游戏外获得一张【金蝉脱壳】（进入弃牌堆时销毁之）。",

  ["#inker_zhantu-invoke"] = "辗途：你可以弃置任意张牌并从游戏外获得一张【金蝉脱壳】",

  ["@@inker_zhantu-inhand"] = "辗途",

  ["$inker_zhantu1"] = "日暮且眠岗上松，散尽千金买东风。",
  ["$inker_zhantu2"] = "这沽来的酒，哪有赊的有味道？",
}

return zhantu